package org.testis.gui.mvc.utilities;

import org.testis.gui.mvc.model.*;
import java.util.LinkedList;

import org.testis.utils.parametermodel.ParameterModel;


/**
 *
 * @author Cahit Emir Erel
 */
public class PageParserObject
{

    private String m_realName;
    private String m_type;
    private LinkedList<ParameterModel> m_parameterList;
    private LinkedList<PageParserObject> m_childs;


    public PageParserObject()
    {
        m_parameterList = new LinkedList<ParameterModel>();
        m_childs = new LinkedList<PageParserObject>();
    }


    public PageParserObject(String realName,
                            String type)
    {
        this();
        m_realName = realName;
        m_type = type;
    }


    public PageParserObject(String realName,
                            String type,
                            LinkedList<ParameterModel> parameterList)
    {
        this();
        m_realName = realName;
        m_type = type;
        m_parameterList = parameterList;
    }


    public PageParserObject(String realName,
                            String type,
                            String[] parameters,
                            String[] values) throws InstantiationException
    {
        this();
        m_realName = realName;
        m_type = type;
        if (parameters.length != values.length) {
            throw new InstantiationException(
                "Number of parameters is not equal to number of values");
        }
        for (int i = 0; i < parameters.length; i++) {
            this.addParameter(parameters[i],
                              values[i]);
        }
    }


    public void addParameter(String name,
                             String value)
    {
        getParameterList().add(new ParameterModel(name,
                                                  value));
    }


    public void addChild(PageParserObject object)
    {
        getChilds().add(object);
    }


    public void removeChild(PageParserObject object)
    {
        getChilds().remove(object);
    }


    public void addParameter(ParameterModel parameter)
    {
        getParameterList().add(parameter);
    }


    public void removeParameter(ParameterModel parameter)
    {
        getParameterList().remove(parameter);
    }


    public void removeParameter(String name,
                                String value)
    {
        for (int i = 0; i < getParameterList().size(); i++) {
            if (getParameterList().get(i).getParameter().equals(name) && getParameterList().
                get(i).
                getValue().equals(value)) {
                getParameterList().remove(i);
                return;
            }
        }
    }


    public String getParameterByName(String name)
    {
        ParameterModel para;
        if (name!=null) {
            for (int i = 0; i < getParameterList().size(); i++) {
                para = getParameterList().get(i);
                if (para.getParameter().equals(name)) {
                    return para.getValue();
                }
            }
        }
        return null;
    }


    public String getChildParameterByName(PageParserObject childObject,
                                          String name)
    {

        for (int i = 0; i < childObject.getParameterList().size(); i++) {
            if (childObject.getParameterList().get(i).getParameter().equals(name)) {
                return childObject.getParameterList().get(i).getValue();
            }
        }
        return null;
    }


    public String getRealName()
    {
        return m_realName;
    }


    public void setRealName(String realName)
    {
        m_realName = realName;
    }


    public String getType()
    {
        return m_type;
    }


    public void setType(String type)
    {
        m_type = type;
    }


    public LinkedList<ParameterModel> getParameterList()
    {
        return m_parameterList;
    }


    public void setParameterList(LinkedList<ParameterModel> parameterList)
    {
        m_parameterList = parameterList;
    }


    public LinkedList<PageParserObject> getChilds()
    {
        return m_childs;
    }


    public void setChilds(LinkedList<PageParserObject> childs)
    {
        m_childs = childs;
    }
}
